package cn.bdqn.asecu.service;

import cn.bdqn.pojo.Audit;
import cn.bdqn.pojo.Department;
import cn.bdqn.pojo.Operationlog;
import cn.bdqn.repository.Asecu_AuditRepository;
import cn.bdqn.repository.Asecu_DepartmentRepository;
import cn.bdqn.repository.Asecu_OperationLogRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 审计机构管理Service
 *
 * @author 8
 */
@Service
public class AsecuAuditService {
    @Resource
    Asecu_AuditRepository asecu_auditRepository;
    @Resource
    Asecu_DepartmentRepository asecu_departmentRepository;
    @Resource
    Asecu_OperationLogRepository asecu_operationLogRepository;

    /**
     * 获取审计机构数据
     * @return
     */
    public List<Audit> getData() {
        List<Audit> audits = asecu_auditRepository.findAll();
        return audits;
    }

    /**
     * 添加
     * @param audit
     */
    @Transactional
    public void add(Audit audit) {
        asecu_auditRepository.save(audit);
        String d = "";
        if (audit.getParent()!=0){
            Audit audit1 = asecu_auditRepository.findById(audit.getParent()).get();
            d = audit1.getAuditName();
        }
        asecu_operationLogRepository.save(new Operationlog("测试用户",new Date(),d,"审计管理","添加审计机构","添加审计机构："+audit.getAuditName()));
    }

    /**
     * 删除
     * @param id
     */
    @Transactional
    public void del(Integer id) {
        Audit audit = asecu_auditRepository.findById(id).get();
        String d ="";
        if (audit.getParent()!=0){
            Audit audit1 = asecu_auditRepository.findById(audit.getParent()).get();
            d = audit1.getAuditName();
        }
        asecu_auditRepository.deleteById(id);
        asecu_auditRepository.deleteByParent(id);
        asecu_operationLogRepository.save(new Operationlog("测试用户",new Date(),d,"审计管理","删除审计机构","删除审计机构："+audit.getAuditName()));
    }

    /**
     * 获取
     * @param id
     * @return
     */
    public Audit getData1(Integer id) {
        Optional<Audit> op = asecu_auditRepository.findById(id);
        if (op.isPresent()==false){
            return null;
        }
        Audit audit = op.get();
        return audit;
    }

    /**
     * 编辑
     * @param audit
     */
    @Transactional
    public void edit(Audit audit) {
        asecu_auditRepository.modify(audit.getCode(),audit.getAuditName(),audit.getId());
        Audit audit1 = asecu_auditRepository.findById(audit.getId()).get();
        String d ="";
        if (audit1.getParent()!=0){
            Audit audit2 = asecu_auditRepository.findById(audit1.getParent()).get();
            d = audit2.getAuditName();
        }
        asecu_operationLogRepository.save(new Operationlog("测试用户",new Date(),d,"审计管理","修改审计机构","修改审计机构："+audit.getAuditName()));
    }

    /**
     * 获取被审计机构
     * @param auditId
     * @return
     */
    public List<Department> getDeptData(Integer auditId) {
        List<Department> departments = asecu_departmentRepository.findByAuditId(auditId);
        return departments;
    }
}
